package com.level2app.ui.home;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.level2app.adapter.HomeCateAdapter;
import com.level2app.base.BaseFragment;
import com.level2app.databinding.FragmentHomeCateBinding;
import com.level2app.ui.home.view.HomeCateFrgViewModel;
import com.level2app.utils.JumpIntent;
import com.level2app.utils.ToastHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by S
 * time 2024/8/19
 */
public class HomeCateFragment extends BaseFragment<FragmentHomeCateBinding, HomeCateFrgViewModel> {
    private int type;
    private HomeCateAdapter cateAdapter, nearByAdapter, todayAdapter, campaignsAdapter;
    private boolean isCreator = true;//是否是creator

    public static HomeCateFragment newInstance(int type) {
        HomeCateFragment homeCateFragment = new HomeCateFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        homeCateFragment.setArguments(args);
        return homeCateFragment;
    }

    @Override
    protected HomeCateFrgViewModel bindModel() {
        return getViewModel(this, HomeCateFrgViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initData() {
        type = getArguments().getInt("type");
        //判断是否是Creator
        if (!isCreator) {
            binding.noCreatorLl.setVisibility(View.VISIBLE);
            binding.normalLl.setVisibility(View.GONE);
            binding.refreshLayout.setEnableRefresh(false);
        } else {
            binding.noCreatorLl.setVisibility(View.GONE);
            binding.normalLl.setVisibility(View.VISIBLE);
            binding.refreshLayout.setEnableRefresh(true);
        }
        switch (type) {
            case 0:
                binding.categoriesTv.setText("Hotel & Resort");
                break;
            case 1:
                binding.categoriesTv.setText("Tourism");
                break;
            case 2:
                binding.categoriesTv.setText("Live Events");
                break;
            case 3:
                binding.categoriesTv.setText("Grand Openings/Launch Product");
                break;
            default:
                binding.categoriesTv.setText("Categories");
                break;
        }
        binding.refreshLayout.setEnableLoadMore(false);
        //分类
        cateAdapter = new HomeCateAdapter();
        binding.cateRv.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        binding.cateRv.setAdapter(cateAdapter);
        //nearBy
        nearByAdapter = new HomeCateAdapter();
        binding.nearRv.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        binding.nearRv.setAdapter(nearByAdapter);
        //Happening today
        todayAdapter = new HomeCateAdapter();
        binding.happenRv.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        binding.happenRv.setAdapter(todayAdapter);
        //campaigns
        campaignsAdapter = new HomeCateAdapter();
        binding.campaignsRv.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        binding.campaignsRv.setAdapter(campaignsAdapter);

        //TODO:测试
        //设置数据
        List<String> data = new ArrayList<>();
        data.add("121123");
        data.add("121123");
        data.add("121123");
        data.add("121123");
        data.add("121123");
        cateAdapter.submitList(data);
        nearByAdapter.submitList(data);
        todayAdapter.submitList(data);
        campaignsAdapter.submitList(data);
    }

    @Override
    protected void initClick() {
        super.initClick();
        viewModel.onDelayClick(binding.creatorBtn, o -> {
            ToastHelper.showToast("Become a Creator");
        });
        //展示全部
        viewModel.onDelayClick(binding.categoriesAllTv, o -> {
            Bundle bundle = new Bundle();
            bundle.putString("title", binding.categoriesTv.getText().toString());
            JumpIntent.jump(getActivity(), CategoriesListActivity.class, bundle);
        });
        viewModel.onDelayClick(binding.nearAllTv, o -> {
            Bundle bundle = new Bundle();
            bundle.putString("title", "NearBy");
            JumpIntent.jump(getActivity(), CategoriesListActivity.class, bundle);
        });
        viewModel.onDelayClick(binding.happenAllTv, o -> {
            Bundle bundle = new Bundle();
            bundle.putString("title", "Happening Today");
            JumpIntent.jump(getActivity(), CategoriesListActivity.class, bundle);
        });
        viewModel.onDelayClick(binding.campaignsAllTv, o -> {
            Bundle bundle = new Bundle();
            bundle.putString("title", "Campaigns");
            JumpIntent.jump(getActivity(), CategoriesListActivity.class, bundle);
        });
        binding.refreshLayout.setOnRefreshListener(refreshLayout -> {
            //刷新
            binding.refreshLayout.finishRefresh();
        });
        cateAdapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {

        });
        nearByAdapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {

        });
        todayAdapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {

        });
        campaignsAdapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {

        });
    }
}